﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using IHISCETeim.Model;
using IHISCETeim.Library;
using IHISCETeim.BLL;


namespace IHISCETeim.Web.MachineSys.MacModel
{
    public partial class OMailLineAdd : PageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (GetRequestValue<string>("ajaxmethod", "") == "add")
            {
                addmenu2();
                
            }
        }

        

        protected void addmenu()
        {

            OMailLineInfo objMail = Request.SerializableObject<OMailLineInfo>();

           // OMailLineController.GET(


            string[] MLTIDS = Request.GetRequestValue<string>("MLTID").Split(new char[]{','});
            string[] SelfCheckResultS = Request.GetRequestValue<string>("SelfCheckResult").Split(new char[] { ',' });
            string[] SelfCheckPeopleS = Request.GetRequestValue<string>("SelfCheckPeople").Split(new char[] { ',' });
            string[] SelfCheckDateS = Request.GetRequestValue<string>("SelfCheckDate").Split(new char[] { ',' });
            string[] SamplingResultS = Request.GetRequestValue<string>("SamplingResult").Split(new char[] { ',' });
            string[] SamplingPeopleS = Request.GetRequestValue<string>("SamplingPeople").Split(new char[] { ',' });
            string[] SamplingDateS = Request.GetRequestValue<string>("SamplingDate").Split(new char[] { ',' });
            string[] SamplingRemarkS = Request.GetRequestValue<string>("SamplingRemark").Split(new char[] { ',' });
            List<OMailLineDetailInfo> listDetail = new List<OMailLineDetailInfo>();
            for (int i = 0; i < MLTIDS.Count();i++ )
            {
                string MLTID = MLTIDS[i];
                //string MLTID in MLTIDS
                if (string.IsNullOrEmpty(MLTID))
                    continue;
                OMailLineDetailInfo itmObj = new OMailLineDetailInfo();
                OMailLineTempleteInfo tempItmObj = allTempList.First(e => e.MLTID == Convert.ToInt32(MLTID));
                if (tempItmObj == null)
                    tempItmObj = new OMailLineTempleteInfo();
                itmObj.GW = tempItmObj.GW;
                itmObj.GWName = tempItmObj.GWName;
                itmObj.ChceckInclude = tempItmObj.ChceckInclude;
                itmObj.InspectionStandards = tempItmObj.InspectionStandards;
                itmObj.TestMethod = tempItmObj.TestMethod;
                itmObj.MTTCode = tempItmObj.MTTCode;

                itmObj.SelfCheckResult = SelfCheckResultS[i];
                itmObj.SelfCheckPeople = SelfCheckPeopleS[i];
                itmObj.SelfCheckDate = SelfCheckDateS[i];
                itmObj.SamplingResult = SamplingResultS[i];
                itmObj.SamplingPeople = SamplingPeopleS[i];
                itmObj.SamplingDate = SamplingDateS[i];
                if (itmObj.SamplingDate != "")
                {
                    try
                    {
                        DateTime DT = Convert.ToDateTime(itmObj.SamplingDate);
                    }
                    catch
                    {
                        ErrorJson("添加失败,相应的检查日期数据出错，请确认");
                        return;
                    }
                }

                itmObj.SamplingRemark = SamplingRemarkS[i];
                //itmObj.m

                listDetail.Add(itmObj);
            }

            //obj.CheckDate=
            this.AddLog(Permission.新增, null, objMail);
            if ( OMailLineController.AddOMailLine(objMail,listDetail))
                SuccessJson("添加成功", this.DWZRef);
            else
                ErrorJson("添加失败,可能相应机号已添加过，请确认", this.DWZRef);
        }
        /// <summary>
        /// 2013 8 27改
        /// </summary>
        protected void addmenu2()
        {

            OMailLineInfo objMail = Request.SerializableObject<OMailLineInfo>();

            // OMailLineController.GET(


            string[] MLTIDS = Request.GetRequestValue<string>("MLTID").Split(new char[] { ',' });
            string[] SelfCheckResultS = Request.GetRequestValue<string>("SelfCheckResult").Split(new char[] { ',' });
            string[] SamplingResultS = Request.GetRequestValue<string>("SamplingResult").Split(new char[] { ',' });

           // string[] SelfCheckPeopleS = Request.GetRequestValue<string>("SelfCheckPeople").Split(new char[] { ',' });
           // string[] SelfCheckDateS = Request.GetRequestValue<string>("SelfCheckDate").Split(new char[] { ',' });
           
            //string[] SamplingPeopleS = Request.GetRequestValue<string>("SamplingPeople").Split(new char[] { ',' });
           // string[] SamplingDateS = Request.GetRequestValue<string>("SamplingDate").Split(new char[] { ',' });
            //string[] SamplingRemarkS = Request.GetRequestValue<string>("SamplingRemark").Split(new char[] { ',' });
            List<OMailLineDetailInfo> listDetail = new List<OMailLineDetailInfo>();
            for (int i = 0; i < MLTIDS.Count(); i++)
            {
                string MLTID = MLTIDS[i];
                //string MLTID in MLTIDS
                if (string.IsNullOrEmpty(MLTID))
                    continue;
                OMailLineDetailInfo itmObj = new OMailLineDetailInfo();
                OMailLineTempleteInfo tempItmObj = allTempList.First(e => e.MLTID == Convert.ToInt32(MLTID));
                if (tempItmObj == null)
                    tempItmObj = new OMailLineTempleteInfo();
                itmObj.GW = tempItmObj.GW;
                itmObj.GWName = tempItmObj.GWName;
                itmObj.ChceckInclude = tempItmObj.ChceckInclude;
                itmObj.InspectionStandards = tempItmObj.InspectionStandards;
                itmObj.TestMethod = tempItmObj.TestMethod;
                itmObj.MTTCode = tempItmObj.MTTCode;

                itmObj.SelfCheckResult = SelfCheckResultS[i];
                itmObj.SamplingResult = SamplingResultS[i];

                itmObj.SelfCheckPeople = GetRequestValue<string>(string.Format("SelfCheckPeople{0}",tempItmObj.GW),"");
                itmObj.SelfCheckDate = GetRequestValue<string>(string.Format("SelfCheckDate{0}", tempItmObj.GW), "");
                itmObj.SamplingPeople = GetRequestValue<string>(string.Format("SamplingPeople{0}", tempItmObj.GW), "");
                itmObj.SamplingDate = GetRequestValue<string>(string.Format("SamplingDate{0}", tempItmObj.GW), "");
                //if (itmObj.SamplingDate != "")
                //{
                //    try
                //    {
                //        DateTime DT = Convert.ToDateTime(itmObj.SamplingDate);
                //    }
                //    catch
                //    {
                //        ErrorJson("添加失败,相应的检查日期数据出错，请确认");
                //        return;
                //    }
                //}

                itmObj.SamplingRemark = GetRequestValue<string>(string.Format("SamplingRemark{0}", tempItmObj.GW), "");
                //itmObj.m

                listDetail.Add(itmObj);
            }

            //obj.CheckDate=
            this.AddLog(Permission.新增, null, objMail);
            if (OMailLineController.AddOMailLine(objMail, listDetail))
                SuccessJson("添加成功", this.DWZRef);
            else
                ErrorJson("添加失败,可能相应机号已添加过，请确认", this.DWZRef);
        }

        protected override Permission[] PowerKey
        {
            get
            {
                return new Permission[] { Permission.新增 };
            }
        }

        protected override string DWZRef
        {
            get { return "OMailLineList"; }
        }


        protected string GetNGSelect(string controlName)
        {
            string selectHtml = "";//;"<select name=\"NG_铁履H前10m\"><option value=\"OK\" selected>OK</option><option value="NG->OK">NG->OK</option><option value="NG">NG</option></select>";
            string[] ngItme = new string[] {  "OK","NG", "NG->OK", };
            foreach (string item in ngItme)
            {
                selectHtml += string.Format("<option value=\"{0}\" {1}>{0}</option>", item, item == "OK" ? "selected" : "");
            }
            return string.Format("<select name=\"{1}\">{0}</select>", selectHtml, controlName);
        }

        private List<OMailLineTempleteInfo> _allTempList;
        protected List<OMailLineTempleteInfo> allTempList
        {
            get
            {
                if(_allTempList==null)
                    _allTempList=OMailLineController.GetAllOMailLineTemplete();
                return _allTempList;
            }
        }
        protected string GetMainLineDetailHtml()
        {
            string html = OMailLineController.DC.Get<string>(OMailLineController.GetOMailLineAddDetailHtml);
            if (!string.IsNullOrEmpty(html))
                return html;
           // List<OMailLineTempleteInfo> allTempList=  OMailLineController.GetAllOMailLineTemplete();
            List<OMailLineTempleteInfo> allGWList = allTempList.Distinct(new OMailLineTempleteDistinctComparer()).OrderBy(e=>e.MLTID).ToList();

            /*
             <tr >
              <td>工位号</td>
              <td >工位名</td>
              <td >检查内容</td>
              <td >检验基准</td>
              <td >检验方法</td>
              <td >自检结果</td>
              <td >自检人</td>
              <td >日期</td>
              <td >抽检结果</td>
              <td >抽检人</td>
              <td >日期</td>
              <td >备注</td>            
             </tr>
             */
            string gwItmHtmlResultItm="";

            string hiddenMLTID = "<input type='hidden' name='MLTID' value='{0}' />";

            string select自检结果 = GetNGSelect("SelfCheckResult");
            string input自检人 = "<input type='text' name='SelfCheckPeople{1}' value='{0}' />";
            string input自检日期 = "<input type='text' name='SelfCheckDate{3}' value='{0}' class='date {1}' inputtag='{2}'/>";
            string select抽检结果 = GetNGSelect("SamplingResult");
            string input抽检人 = "<input type='text' name='SamplingPeople{1}' value='{0}' />";
            string input抽检日期 = "<input type='text' name='SamplingDate{3}' value='{0}' class='date {1}' inputtag='{2}' />";
            string input备注 = "<input type='text' name='SamplingRemark{0}' value='' />";

            int sametrint = 0;
            foreach (OMailLineTempleteInfo tempItm in allGWList)
            {
                
                List<OMailLineTempleteInfo> gwItmList= allTempList.FindAll(e => e.GW == tempItm.GW);
                int rowspan = gwItmList.Count();
                string gwItmHtml = "<tr ><td >{0}</td><td >{1}</td><td >{2}{3}</td></tr>";
                string gwItmHtml2 = "<tr><td rowspan={0}>{1}</td><td  rowspan={0}>{2}</td><td >{3}</td><td >{4}</td><td rowspan={0}>{5}</td><td rowspan={0}>{6}</td><td >{7}</td><td rowspan={0}>{8}</td><td  rowspan={0}>{9}</td><td rowspan={0}>{10}{11}</td></tr>";
                int tagI=0;
              
                foreach (OMailLineTempleteInfo gwItm in gwItmList)
                { 
                    if(tagI==0)
                        gwItmHtmlResultItm += string.Format(gwItmHtml2, rowspan, gwItm.GW, gwItm.GWName, gwItm.ChceckInclude,select自检结果, string.Format(input自检人, gwItm.SelfCheckPeople, gwItm.GW), string.Format(input自检日期, "", "firsttrSelfCheckDate", "SelfCheckDate" + sametrint.ToString(), gwItm.GW), select抽检结果, string.Format(input抽检人, gwItm.SamplingPeople, gwItm.GW), string.Format(input抽检日期, "", "firsttrSamplingDate", "SamplingDate" + sametrint.ToString(), gwItm.GW),string.Format(input备注,gwItm.GW), string.Format(hiddenMLTID, gwItm.MLTID));
                    else
                        gwItmHtmlResultItm += string.Format(gwItmHtml, gwItm.ChceckInclude, select自检结果,  select抽检结果,  string.Format(hiddenMLTID, gwItm.MLTID));
                    tagI++;
                }
                sametrint++;
            }
            html = gwItmHtmlResultItm;

            OMailLineController.DC.Add(OMailLineController.GetOMailLineAddDetailHtml, html);
            return html;

        }

    }
}